﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace treeView.PLC
{
    public enum PLCType
    {
        OPC,
        ADS
    }
    public class PLCVar
    {
        public bool IsUpdate = false;
        public bool NeedRegistor = false;
        public string VarAddress = string.Empty;
        public string RegistedAdd = string.Empty;
        public string Value = "#";
    }
    public interface IPLC
    {
        /// <summary>
        /// PLC名字
        /// </summary>
        string Name { get; }
        /// <summary>
        /// PLC序号
        /// </summary>
        int Index { get; }
        /// <summary>
        /// 连接状态
        /// </summary>
        bool Connected { get; }
        /// <summary>
        /// PLC读变量列表
        /// </summary>
        Dictionary<string, PLCVar> PlcVarList { get; }
        /// <summary>
        /// 设置PLC连接地址
        /// </summary>
        void SetConnectionStr(string str);
        /// <summary>
        /// 连接PLC
        /// </summary>
        void Connect();
        /// <summary>
        /// 开始重连
        /// </summary>
        event EventHandler ReconnectStarting;
        /// <summary>
        /// 更新PLC变量的值
        /// </summary>
        void Update();
        /// <summary>
        /// 关闭连接
        /// </summary>
        void DisconnectPLC();
        /// <summary>
        /// 初始化PLC的变量列表
        /// </summary>
        void InitialPLCVarList();

        /// <summary>
        /// vars可更新 出于对通讯压力的考虑 
        /// </summary>
        /// <param name="vars"></param>
        void EnablePLCVar(string pageId, bool hasQuote, List<string> vars, bool isRead = true);

        /// <summary>
        /// vars不可更新 出于对通讯压力的考虑 
        /// </summary>
        /// <param name="vars"></param>
        void DisablePLCVar(string pageId, List<string> vars);

        /// <summary>
        /// 向PLC变量地址写入值
        /// </summary>
        /// <param name="VarAddress"></param>
        /// <param name="type"></param>
        /// <param name="value"></param>
        void SetPLCVar(string VarAddress, string value);



        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="vars"></param>
        /// <returns></returns>
        List<string> ReadData(List<string> vars);
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="var"></param>
        /// <returns></returns>
        string ReadData(string var);
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="var"></param>
        /// <returns></returns>
        T ReadData<T>(string var);

        /// <summary>
        /// 注册地址
        /// </summary>
        /// <param name="vars"></param>
        /// <returns></returns>
        List<string> RegisterVar(List<string> vars);
        /// <summary>
        /// 注销注册地址
        /// </summary>
        /// <param name="vars"></param>
        /// <returns></returns>
        void UnregisterVar(List<string> vars);

        /// <summary>
        /// 清除已注册地址
        /// </summary>
        void RemoveRegistedAdd();
        /// <summary>
        /// 注册地址
        /// </summary>
        void RegistorNode();
        /// <summary>
        /// 状态
        /// </summary>
        event EventHandler<string> Alive;

        string State { get; }
    }
}
